package org.ee.system.domain;

import jakarta.persistence.*;
import org.ee.framework.jpa.base.AbstractEntity;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 角色表
 * @Author jokr
 * @Date 2025/8/25 下午5:14
 */
@Entity
@Table(name="sys_role")
@DynamicUpdate
@DynamicInsert
@Comment("角色表")
public class Role extends AbstractEntity<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Comment("主键")
    private Long id;

    @Comment("角色名称")
    private String name;

    @Comment("角色编码")
    private String code;

    @Comment("角色状态")
    private Integer status;

    @Comment("角色描述")
    private String description;

    @Comment("排序字段")
    private Integer sortNum;

    //关联映射
    @OneToMany(mappedBy = "role")
    private Set<UserRole> userRoles = new HashSet<>(); // 角色和用户的关联关系

    /**
     * 角色菜单映射
     */
    @ManyToMany
    @JoinTable(
            name = "sys_role_menu",
            joinColumns = @JoinColumn(name = "role_id",
                    columnDefinition = "bigint comment '角色ID'",
                    foreignKey = @ForeignKey(value=ConstraintMode.NO_CONSTRAINT)
            ),
            inverseJoinColumns = @JoinColumn(name = "menu_id",
                    columnDefinition = "bigint comment '菜单ID'",
                    foreignKey = @ForeignKey(value=ConstraintMode.NO_CONSTRAINT)
            ),
            uniqueConstraints = @UniqueConstraint(columnNames = {"role_id","menu_id"})
    )
    @Comment("角色菜单表")
    private List<Menu> menus;

    /**
     * 角色权限映射
     * //只能使用唯一约束 ，如果要双主键，则需要复合主键的方式
     * @see org.ee.system.domain.UserRoleId
     * @see org.ee.system.domain.UserRole
     */
    @ManyToMany
    @JoinTable(
            name = "sys_role_permission",
            joinColumns = @JoinColumn(name = "role_id",
                    columnDefinition = "bigint comment '角色ID'",
                    foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)
            ),
            inverseJoinColumns = @JoinColumn(name = "permission_id",
                    columnDefinition = "bigint comment '权限ID'",
                    foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)
            ),
            uniqueConstraints = @UniqueConstraint(columnNames = {"role_id","permission_id"})
    )
    @Comment("角色权限表")
    private List<Permission> permissions;

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getSortNum() {
        return sortNum;
    }

    public void setSortNum(Integer sortNum) {
        this.sortNum = sortNum;
    }

    public Set<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(Set<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }
}
